#include "sw_p2p_transfer.h"
#include "../log/log_err.h"
#include "crc/crc16.h"
#include <strings.h>

static int msgId = 0;

static int p2p_strncpy(char * __dest, const char * __src, size_t __n)
{
    int i = 0;
    for (i = 0; i < (__n - 1) && __src[i] != '\0'; i++) {
        __dest[i] = __src[i];
    }
    __dest[i] = '\0';
    return i + 1;
}

/***************************************解析数据************************************/

bool sw_p2p_analyse_comm_buffer(struct ring_arrays* array)
{
    struct uint8_array data;
    P2P_PACKET_DATA net_packet_data;
    uint16_t crc16_value;
    uint8_t* buffer = nullptr;
    int i = 0, k = 0;
    int length = 0;
    while (true)
    {
        i = 0, k = 0;
        bzero(&net_packet_data, sizeof(net_packet_data));
        data = ring_arrays_get_buffer(array);
        buffer = data.data;
        length = data.length;

        if (buffer == nullptr || length < P2P_MIN_FRAME_LEN) {
            return false;
        }
        /* 包头判断 */
        ANALYSE_SET_VALUE(net_packet_data.start_data, 0) = buffer[i++];
        ANALYSE_SET_VALUE(net_packet_data.start_data, 1) = buffer[i++];
        if (net_packet_data.start != P2P_PACKET_HEADER) {
            ring_arrays_set_offset(array, 1);
            SIGNWAY_LOGE("包头错误");
            goto CONTINUE;
        }
        /* 获取包长度 */
        ANALYSE_SET_VALUE(net_packet_data.length_data, 0) = buffer[i++];
        ANALYSE_SET_VALUE(net_packet_data.length_data, 1) = buffer[i++];
        ANALYSE_SET_VALUE(net_packet_data.length_data, 2) = buffer[i++];
        ANALYSE_SET_VALUE(net_packet_data.length_data, 3) = buffer[i++];
        if (net_packet_data.length > length) {
            SIGNWAY_LOGW("长度不够%d:%d", net_packet_data.length, length);
            return FALSE;
        }
        /* 数据类型 */
        net_packet_data.type = buffer[i++];
        /* 保留位 */
        ANALYSE_SET_VALUE(net_packet_data.empty, 0) = buffer[i++];
        ANALYSE_SET_VALUE(net_packet_data.empty, 1) = buffer[i++];
        ANALYSE_SET_VALUE(net_packet_data.empty, 2) = buffer[i++];
        /* 包体长度 */
        net_packet_data.package_body_len = net_packet_data.length - P2P_ADDITIONAL_LENGTH;
        /* 包体 */
        net_packet_data.package_body = (uint8_t *)malloc(net_packet_data.package_body_len + 1);
        for (k = 0; k < net_packet_data.package_body_len; k++) {
            net_packet_data.package_body[k] = buffer[i++];
        }
        /* 获取CRC */
        ANALYSE_SET_VALUE(net_packet_data.crc_data, 0) = buffer[i++];
        ANALYSE_SET_VALUE(net_packet_data.crc_data, 1) = buffer[i++];
        crc16_value = GetCRC16(buffer, i - 2);
        if (crc16_value != net_packet_data.crc) {
            ring_arrays_set_offset(array, i - 2);
            SIGNWAY_LOGE("获取CRC 失败！%04X", crc16_value);
            goto CONTINUE;
        }
        /* 解析成功 移动位置 */
        ring_arrays_set_offset(array, i);
        break;
CONTINUE:
        if (net_packet_data.package_body != nullptr) {
            free(net_packet_data.package_body);
        }
        continue;
    }

    switch (net_packet_data.type) {
    case P2P_PACKET_TYPE_JSON: {
        net_packet_data.package_body[net_packet_data.package_body_len] = '\0';
        printf("%s", net_packet_data.package_body);
    } break;
    }

    free(net_packet_data.package_body);
    return true;
}